<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :data="defaultWhere" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        :where="defaultWhere"
      >
        <template #toolbar>
          <el-button type="success" size="" @click="openPay()">
            批量付款
          </el-button>
        </template>
        <template #plate_number="{ row }">
          <span class="blue pointer" @click="openDetail(row)">{{
            row.plate_number
          }}</span>
        </template>
        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>
        <template #images="{ row }">
          <span class="pointer blue" @click="openImages(row, 'images')"
            >查看</span
          >
        </template>
        <template #pay_images="{ row }">
          <span class="pointer blue" @click="openImages(row, 'pay_images')"
            >查看</span
          >
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="success" size="small" @click="openPay(row)">
              付款
            </el-button>
            <el-button type="danger" size="small" @click="reject(row)">
              不通过
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 已付款操作 -->
    <pay v-model="showPay" :data="current" @done="reload" />
    <Reject v-model="showReject" :data="current" @done="reload" />
    <images v-model="showImages" :data="current" :img-type="imagesType" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './components/search.vue';
  import Pay from './components/pay.vue';
  import Reject from './components/reject.vue';
  import Images from './components/images.vue';

  import { EleMessage } from 'ele-admin-plus/es';
  import { pageVehicleIncomeExpense } from '@/api/company';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push({
      type: 'selection',
      columnKey: 'selection',
      width: 50,
      align: 'center',
      fixed: 'left'
    });

    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 85,
        align: 'center'
      },
      {
        width: 100,
        slot: 'status',
        label: '状态'
      },
      {
        width: 150,
        prop: 'cate',
        label: '费用分类'
      },
      {
        width: 120,
        prop: 'amount',
        label: '付款金额'
      },
      {
        prop: 'remark',
        label: '备注'
      },
      {
        width: 88,
        align: 'center',
        slot: 'images',
        label: '相关材料'
      }
    );
    // 待付款的显示提交人
    if (status.value === 0 || status.value === 90) {
      cols.push(
        {
          width: 80,
          prop: 'create_user',
          label: '提交人'
        },
        {
          width: 110,
          prop: 'createTime',
          label: '提交时间'
        }
      );
    }
    // 已拒绝的显示时间原因
    if (status.value === 90) {
      cols.push(
        {
          width: 80,
          prop: 'create_user',
          label: '拒绝人'
        },
        {
          width: 110,
          prop: 'rejectTime',
          label: '拒绝时间'
        },
        {
          prop: 'reject_reason',
          label: '拒绝原因'
        }
      );
    }

    // 已完成的菜显示付款信息
    if (status.value == 20) {
      cols.push(
        {
          width: 100,
          align: 'center',
          slot: 'pay_images',
          label: '付款图片'
        },
        {
          width: 130,
          prop: 'belongs_date',
          label: '付款日期'
        },
        {
          width: 80,
          prop: 'pay_user',
          label: '付款人'
        }
      );
    }

    cols.push(
      {
        width: 70,
        align: 'center',
        prop: 'area',
        label: '区域'
      },
      {
        width: 100,
        slot: 'plate_number',
        label: '车牌号码'
      },
      {
        width: 230,
        prop: 'company',
        label: '车队'
      }
    );
    // 待付款的才有操作
    if (status.value === 0) {
      cols.push({
        columnKey: 'action',
        label: '操作',
        slot: 'action',
        width: 150,
        align: 'center',
        fixed: 'right'
      });
    }

    return cols;
  });

  /** 当前编辑数据 */
  const current = ref(null);

  /** 付款 */
  const showPay = ref(false);
  const openPay = (row = null) => {
    const rows = row == null ? selections.value : [row];

    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    const isConsistent = rows.every((item) => item.io_type === rows[0].io_type);
    if (!isConsistent) {
      EleMessage.warning('请选择收支类型一致的数据进行操作');
      return false;
    }

    current.value = rows;
    showPay.value = true;
  };
  /** 拒绝付款 */
  const showReject = ref(false);
  const reject = (row) => {
    showReject.value = true;
    current.value = row;
  };

  /** 查看图片 */
  const showImages = ref(false);
  const imagesType = ref('');
  const openImages = (row, type) => {
    current.value = row;
    imagesType.value = type;
    showImages.value = true;
  };
  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageVehicleIncomeExpense({
      ...where,
      ...orders,
      page,
      limit,
      io_type: '支出'
    });
  };
  const defaultWhere = ref({
    cate: '不限',
    status: 0,
    io_type: '支出'
  });

  /** 搜索 */
  const costType = ref('支出');
  const status = ref(0);
  const reload = (where) => {
    costType.value = where ? where.io_type : '收入';
    status.value = where ? where.status : 0;
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
    console.log(costType.value, 'wtfccccccccccccccc?');
  };
</script>
